package collection;

import com.google.common.base.Predicate;
import com.google.common.collect.*;

import java.util.*;

/**
 * @author Created by chenxin34 on 2018/10/11 下午1:10.
 */
public class GuavaMultiMapTest {
    public static void main(String[] args) {
        Multimap<Long, Long> multimap = HashMultimap.create();
        multimap.put(1L, 10L);
        multimap.put(1L, 20L);
        System.out.println(multimap.get(1L));

        multimap.put(2L, 100L);
        multimap.put(2L, 200L);

        List<Long> values = Lists.newArrayList(FluentIterable.from(multimap.values()));
        System.out.println(values);

        System.out.println("multimap");
        System.out.println(multimap);

        Multimap<Long, Long> afterFilter = Multimaps.filterEntries(multimap, new Predicate<Map.Entry<Long, Long>>() {
            @Override
            public boolean apply(Map.Entry<Long, Long> longLongEntry) {
                return longLongEntry.getValue() != 100;
            }
        });

        System.out.println("afterFilter originMap");
        System.out.println(multimap);

        System.out.println("afterFilter newMap");
        System.out.println(afterFilter);

        System.out.println("******** testValueRemove ******");
        testValueRemove();

        System.out.println("******** testRemove ******");
        testRemove();

        System.out.println("******** testRemoveAll ******");
        testRemoveAll();

    }

    private static void testValueRemove() {
        Multimap<Long, Long> multimap = HashMultimap.create();
        multimap.put(1L, 2L);
        multimap.put(1L, 3L);
        System.out.println(multimap);

        Collection<Long> collection = multimap.get(1L);
        System.out.println(collection);
        collection.remove(2L);
        System.out.println(collection);
        System.out.println(multimap);

    }

    private static void testRemove() {
        Multimap<Long, Long> waybillVicinalRiders = HashMultimap.create();
        waybillVicinalRiders.put(1L, 2L);
        waybillVicinalRiders.put(1L, 3L);

        Set<Long> optimizedRiders = new HashSet<>();
        optimizedRiders.add(2L);
        for (Long wid : waybillVicinalRiders.keySet()) {
            for (Long riderId : optimizedRiders) {
                waybillVicinalRiders.remove(wid, riderId);
            }
        }
        System.out.println(waybillVicinalRiders);
    }

    private static void testRemoveAll() {
        Multimap<Long, Long> waybillVicinalRiders = HashMultimap.create();
        waybillVicinalRiders.put(1L, 2L);
        waybillVicinalRiders.put(1L, 3L);

        List<Long> optimizedRiders = new ArrayList<>();
        optimizedRiders.add(2L);
        for (Long wid : waybillVicinalRiders.keySet()) {
            Collection<Long> pushRiders = waybillVicinalRiders.get(wid);
            pushRiders.removeAll(optimizedRiders);
        }
        System.out.println(waybillVicinalRiders);
    }
}
